package health.lm.com.shop.fragment;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.vondear.rxtool.view.RxToast;
import com.zzhoujay.richtext.RichText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import health.lm.com.component_base.base.mvp.fragment.XLazyFragment;
import health.lm.com.component_base.helper.ImageLoaderHelper;
import health.lm.com.shop.R;
import health.lm.com.shop.R2;
import health.lm.com.shop.adapter.HLMyCompanyLeaderAdapter;
import health.lm.com.shop.entity.HLMyCompanyEntity;

/**
 * Created by Android Studio .
 * Project：hlian health.lm.com.shop.fragment
 * Date：2019/8/13 9:10
 * Introduce：我的商会Fragment
 */
public class HLMyCompanyFragment extends XLazyFragment {
    @BindView(R2.id.rv_list)
    RecyclerView mRvList;
    @BindView(R2.id.imageView10)
    ImageView mImageView10;
    @BindView(R2.id.textView42)
    TextView mTextView42;
    private HLMyCompanyLeaderAdapter mLeaderAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.hl_fragment_my_company;
    }

    @Override
    public Object newP() {
        return null;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        if (null == bundle) return;
        int type = bundle.getInt("type");
        switch (type) {
            case 1:
                mImageView10.setVisibility(View.GONE);
                mTextView42.setVisibility(View.GONE);
                ArrayList<HLMyCompanyEntity.LeaderEntity> leaderEntities = bundle.getParcelableArrayList("data");
                initAdapter(leaderEntities);
                return;
            case 2:
                String image = bundle.getString("data");
                mRvList.setVisibility(View.GONE);
                mTextView42.setVisibility(View.GONE);
                ImageLoaderHelper.getInstance().load(context, image, mImageView10);
                break;
            case 3:
                String text = bundle.getString("data");
                mRvList.setVisibility(View.GONE);
                mImageView10.setVisibility(View.GONE);
                RichText.initCacheDir(context);
                RichText.fromHtml(text).into(mTextView42);
                //mTextView42.setText(text);
                break;
        }
    }

    private void initAdapter(List<HLMyCompanyEntity.LeaderEntity> pList) {
        mLeaderAdapter = new HLMyCompanyLeaderAdapter(pList);
        mRvList.setLayoutManager(new GridLayoutManager(context, 3));
        mRvList.setNestedScrollingEnabled(false);
        mRvList.setAdapter(mLeaderAdapter);
        mLeaderAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            RxToast.normal(pList.get(position).getName());
        });

        View footView = LayoutInflater.from(context).inflate(R.layout.layout_text, null);
        TextView textView = footView.findViewById(R.id.textView);
        Drawable drawable = ContextCompat.getDrawable(context, R.drawable.ic_keyboard_arrow_right_black_24dp);
        textView.setGravity(Gravity.CENTER);
        textView.setCompoundDrawablesRelativeWithIntrinsicBounds(null,null,drawable,null);
        textView.setText("查看更多");
        footView.setOnClickListener(v->{});
        mLeaderAdapter.addFooterView(footView);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (null != mLeaderAdapter) mRvList.setAdapter(mLeaderAdapter);
    }
}
